package cronjob

import (
	"context"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
	"go-zero-admin/application/applet/api/internal/svc"
	"go-zero-admin/application/applet/api/internal/types"
	job "go-zero-admin/application/job/rpc/pb"
)

type SearchCronListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 查询定时任务
func NewSearchCronListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchCronListLogic {
	return &SearchCronListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SearchCronListLogic) SearchCronList(req *types.SearchScheduleReq) (resp *types.SearchScheduleRes, err error) {
	var page job.PageRequest
	if req.PageNo == 0 {
		page.PageNo = 1
	}
	if req.PageSize == 0 {
		page.PageSize = 10
	}
	page.PageNo = req.PageNo
	page.PageSize = req.PageSize

	res, err := l.svcCtx.JobCronSvc.SearchCronList(l.ctx, &job.SearchScheduleReq{PageRequest: &page})
	if err != nil {
		return nil, err
	}
	resp = &types.SearchScheduleRes{}
	_ = copier.Copy(resp, &res)
	return resp, nil
}
